package com.bsoft.gol.hcs.specialist.dao;

import com.alibaba.fastjson.support.geo.LineString;
import com.bsoft.gol.hcs.satisfaction.entity.PatientSatisfactionSurvey;
import com.bsoft.gol.hcs.specialist.entity.DuanLian;
import com.bsoft.gol.hcs.utils.CommonUtils;
import ctd.persistence.annotation.DAOMethod;
import ctd.persistence.annotation.DAOParam;
import ctd.persistence.support.hibernate.HibernateSupportDelegateDAO;
import ctd.persistence.support.hibernate.template.AbstractHibernateStatelessResultAction;
import ctd.persistence.support.hibernate.template.HibernateSessionTemplate;
import ctd.persistence.support.hibernate.template.HibernateStatelessResultAction;
import ctd.spring.boot.annotation.SsdevDao;
import ctd.util.converter.ConversionUtils;
import org.hibernate.Query;
import org.hibernate.StatelessSession;
import org.hibernate.transform.Transformers;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@SsdevDao(serviceId = "duanLianDAOService")
public abstract class DuanLianDAO extends HibernateSupportDelegateDAO<DuanLian> {
    public DuanLianDAO() {
        this.setKeyField("id");
    }


    @DAOMethod(sql = " from  DuanLian where id =:id ")
    public abstract DuanLian getDurlMapping(@DAOParam("id") Long id);

    public List<DuanLian> getForCreateTime(@DAOParam("createTime") Date createTime, @DAOParam("newTime") Date newTime, @DAOParam("phone") String phone) {
        HibernateStatelessResultAction<List<DuanLian>>  action = new AbstractHibernateStatelessResultAction<List<DuanLian>>() {
            @Override
            public void execute(StatelessSession statelessSession) throws Exception {
                StringBuffer buffer = new StringBuffer();
                buffer.append("select * from  durl_mapping where phone= :phone  and createTime<= :newTime AND createTime>= :createTime ");
                Query query = statelessSession.createSQLQuery(buffer.toString());

                query.setParameter("phone", phone);
                query.setParameter("createTime", createTime);
                query.setParameter("newTime", newTime);

                query.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
                List<Map> list = query.list();
                List<DuanLian> voList = new ArrayList<>();
                if (CommonUtils.isNotEmpty(list)) {
                    for (Map map : list) {
                        voList.add(ConversionUtils.convert(map, DuanLian.class));
                    }
                }
                setResult(voList);
            }
        };
        HibernateSessionTemplate.instance().execute(action);
        return action.getResult();
    }

    public static void main(String[] args) {
        System.out.println(new Date(new Date().getTime() - 168*60*60*1000));
    }
}


